/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StatusPanel.java
 *
 * Created on 07 Feb 2011, 11:52:40 AM
 */
package za.co.pas.spaceandtime.component;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import za.co.pas.business.abstracts.AbstractDataModel;
import za.co.pas.business.message.MessageObj;
import za.co.pas.business.message.StatusMessageObject;
import za.co.pas.business.abstracts.AbstractView;
import za.co.pas.business.constant.Constant;
import za.co.pas.spaceandtime.view.dlg.ErrorListDialog;

/**
 *
 * @author alabuschagne
 */
public class StatusPanel extends JPanel implements AbstractView, PropertyChangeListener
{

    private static final int NUMBER_OF_TICKS_MSG = 20;
    private static final Queue<StatusMessageObject> queue = new LinkedList<StatusMessageObject>();
    private int noNewMessageCount = NUMBER_OF_TICKS_MSG;
    private boolean stopDueToErrorMsg = false;
    private StatusLightsPanel statusLightsPanel;
    private Timer timer;
    private TimerTask timerTask = new TimerTask()
    {
        @Override
        public void run()
        {
            SwingUtilities.invokeLater(new Runnable()
            {

                @Override
                public void run()
                {
                    if (queue.isEmpty())
                    {
                        if (!stopDueToErrorMsg)
                        {
                            noNewMessageCount--;
                            if (noNewMessageCount == 0)
                            {
                                noNewMessageCount = NUMBER_OF_TICKS_MSG;
                                updateMessage(" ");
                            }
                        }
                    }
                    else
                    {
                        noNewMessageCount = NUMBER_OF_TICKS_MSG;
                        if (!stopDueToErrorMsg)
                        {
                            StatusMessageObject smo = queue.poll();
                            updateMessage(smo.getMessage());
                            stopDueToErrorMsg = smo.getStatus() == StatusMessageObject.SMO_ERROR;
                        }
                        if ((stopDueToErrorMsg) && (!moreButton.isVisible()))
                        {
                            moreButton.setVisible(true);
                        }
                    }
                }
            });
        }
    };

    /** Creates new form StatusPanel */
    public StatusPanel()
    {
        initComponents();
        Constant.mvc.addPropertyChangeListener(this);
        statusLightsPanel = new StatusLightsPanel();
        add(statusLightsPanel, java.awt.BorderLayout.EAST);
        moreButton.setVisible(false);
        timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 10, 200);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        messageLabel = new javax.swing.JLabel();
        moreButton = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setLayout(new java.awt.BorderLayout(10, 0));

        messageLabel.setText("Loading...");
        messageLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        messageLabel.setOpaque(true);
        add(messageLabel, java.awt.BorderLayout.CENTER);

        moreButton.setText("?");
        moreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moreButtonActionPerformed(evt);
            }
        });
        add(moreButton, java.awt.BorderLayout.WEST);
    }// </editor-fold>//GEN-END:initComponents

    private void moreButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_moreButtonActionPerformed
    {//GEN-HEADEREND:event_moreButtonActionPerformed
        ErrorListDialog eld = new ErrorListDialog(Constant.getParentFrame(this), true);
        eld.setErrors((StatusMessageObject[]) queue.toArray());
        eld.setVisible(true);
        queue.clear();
    }//GEN-LAST:event_moreButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel messageLabel;
    private javax.swing.JButton moreButton;
    // End of variables declaration//GEN-END:variables

    public void addMessage(StatusMessageObject smo)
    {
        synchronized (queue)
        {
            queue.add(smo);
        }
    }

    public void updateMessage(String msg)
    {
        final String msg2 = msg;
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                messageLabel.setText(msg2);
            }
        });
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().compareTo(MessageObj.STATUS_MESSAGE) == 0)
        {
            if (evt.getNewValue() instanceof StatusMessageObject)
            {
                StatusMessageObject smo = (StatusMessageObject) evt.getNewValue();
                addMessage(smo);
            }
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().compareTo(MessageObj.STATUS_MESSAGE) == 0)
        {
            if (evt.getNewValue() instanceof StatusMessageObject)
            {
                StatusMessageObject smo = (StatusMessageObject) evt.getNewValue();
                addMessage(smo);
            }
        }
    }

    public void addPropChangeListener(AbstractDataModel adm)
    {
        adm.addPropertyChangeListener(this);
    }

    @Override
    public boolean isDirty()
    {
        return false;
    }

    @Override
    public String getExtraTitle()
    {
        return null;
    }
}
